package ordinaryArray;

import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;

import java.util.HashMap;
import java.util.Map;

/*
 *@description 罗马数字转整数
 *@author hkfan
 *@version 1.0
 *@create 2025/5/26 11:10
 */
public class LeetCode_13 {

    class Node{
        int index;
        int value;

        public Node(int index, int value) {
            this.index = index;
            this.value = value;
        }
    }

    Map<Character,Node> map;


    @Test
    public void test(){
        System.out.println(romanToInt("III"));
    }

    public int romanToInt(String s) {
        //0. 特殊情况
        if (s.length()==0){
            return 0;
        }
        //1. 定义初始化
        initMap();
        int res = 0;
        //2. 循环处理
        for (int i = 0; i <= s.length()-2; i++) {
            if (map.get(s.charAt(i)).index < map.get(s.charAt(i+1)).index){     //左侧小于情况
                res -= map.get(s.charAt(i)).value;
            }else {
                res += map.get(s.charAt(i)).value;
            }
        }
        //3. 最后一个元素处理
        res += map.get(s.charAt(s.length()-1)).value;
        return res;
    }

    private void initMap() {
        map=new HashMap<>();
        map.put('I',new Node(0,1));
        map.put('V',new Node(1,5));
        map.put('X',new Node(2,10));
        map.put('L',new Node(3,50));
        map.put('C',new Node(4,100));
        map.put('D',new Node(5,500));
        map.put('M',new Node(6,1000));
    }


}
